import axios from 'axios'
import { get, merge } from 'lodash'
import { Message, Notification } from 'element-ui'
import utils from '@/utils'
import env from '@/env'
import qs from 'qs'

// 记录和显示错误
function log(error) {
    if (env.NODE_ENV === 'development') {
        utils.log.danger('>>>>>> Error >>>>>>')
        console.error(error)
    }
    Message({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
    })
}

const service = axios.create({})

service.interceptors.response.use(
    async response => {
        const dataAxios = response.data
        if (dataAxios.code === 200) {
            // 正常返回数据
            return dataAxios.data
        } else if (!dataAxios.code) {
            //    解析文档流并下载表格
            const link = document.createElement('a')
            let blob = new Blob([dataAxios], {
                type: 'application/vnd.ms-excel'
            })
            link.style.display = 'none'
            //设置连接
            link.href = URL.createObjectURL(blob)
            link.download = 'data_table.xlsx'
            document.body.appendChild(link)
            //模拟点击事件
            link.click()
        } else {
            const error = new Error(dataAxios.msg)
            // 需要重新登录
            // 50008 - 无效的 token
            // 50012 - 其它客户端登录
            // 50014 - token 过期
            if ([401, 403, 50014].indexOf(dataAxios.code) >= 0) {
                Notification.error({
                    title: '身份验证失败',
                    message: '请重新登录'
                })
            } else {
                log(error)
            }
            return Promise.reject(error)
        }
    },
    // eslint-disable-next-line complexity
    error => {
        if (error && error.response) {
            switch (error.response.status) {
                case 400:
                    error.message = '请求错误'
                    break
                case 401:
                    error.message = '未授权，请登录'
                    break
                case 403:
                    error.message = '拒绝访问'
                    break
                case 404:
                    error.message = `请求地址出错: ${error.response.config.url}`
                    break
                case 408:
                    error.message = '请求超时'
                    break
                case 500:
                    error.message = '服务器内部错误'
                    break
                case 501:
                    error.message = '服务未实现'
                    break
                case 502:
                    error.message = '网关错误'
                    break
                case 503:
                    error.message = '服务不可用'
                    break
                case 504:
                    error.message = '网关超时'
                    break
                case 505:
                    error.message = 'HTTP版本不受支持'
                    break
                default:
                    break
            }
        }
        log(error)
        return Promise.reject(error)
    }
)
function isLoginPath(path) {
    return path === '/api/v1/login'
}
export async function request(config) {
    const token = utils.cookies.get('token')
    if (!token && !isLoginPath(config.url)) {
        console.log('退出登录')
    }
    let configDefault = {
        headers: {
            Authorization: token,
            'Content-Type': get(
                config,
                'header.Content-Type',
                // 'application/json'
                'application/x-www-form-urlencoded'
            )
        },
        timeout: 60000,
        baseURL: env.VUE_APP_BASE_API,
        data: {}
    }
    if (
        get(
            config,
            'header.Content-Type',
            'application/x-www-form-urlencoded'
        ) === 'application/x-www-form-urlencoded'
    ) {
        configDefault.transformRequest = [
            function(data) {
                data = qs.stringify(data)
                return data
            }
        ]
    }
    return service(Object.assign(configDefault, config))
}
